package com.example.cofeed.activity.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.ActivityUtils;
import com.example.cofeed.R;
import com.example.cofeed.activity.DemandIssueActivity;
import com.example.cofeed.activity.LoginActivity;
import com.example.cofeed.activity.SupplyDemandActivity;
import com.example.cofeed.adapter.PagerAdapter;
import com.example.cofeed.base.BaseFragment;
import com.example.cofeed.constant.IntentKeyConstant;
import com.example.cofeed.even.RefreshEven;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class DemandFragment extends BaseFragment {
    @BindView(R.id.tabs)
    SegmentTabLayout mTabs;
    @BindView(R.id.view_pager)
    ViewPager mViewPager;
    @BindView(R.id.rl_gone)
    RelativeLayout mRlGone;
    private int mPosition;

    private ArrayList<Fragment> mFragments = new ArrayList<>();

    @Override
    protected View initViews(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_demand, container, false);
        return view;
    }

    @Override
    protected void initData() {

        mFragments.add(DemandSubTabFragment.newInstance(0));
        mFragments.add(DemandSubTabFragment.newInstance(1));
        mFragments.add(DemandSubTabFragment.newInstance(2));
        mFragments.add(DemandSubTabFragment.newInstance(3));

        List<String> titles = new ArrayList<>();
        titles.add("全部");
        titles.add("供应");
        titles.add("求购");
        titles.add("招商");

        mViewPager.setAdapter(new PagerAdapter(getChildFragmentManager(), mFragments, titles));

        mTabs.setTabData(titles.toArray(new String[titles.size()]));
        mViewPager.setOffscreenPageLimit(titles.size() - 1);
        mTabs.setOnTabSelectListener(new OnTabSelectListener() {

            @Override
            public void onTabSelect(int position) {
                mPosition = position;
                mViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTabs.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @OnClick({R.id.iv_add, R.id.iv_close, R.id.tv_fbgq, R.id.tv_fbqg, R.id.tv_zs, R.id.tv_wdgq, R.id.rl_gone})
    public void onViewClicked(View view) {
        if (!isLogin()){
            ActivityUtils.startActivity(LoginActivity.class);
            return;
        }
        Intent intent = null;
        getView().findViewById(R.id.iv_add).setVisibility(View.VISIBLE);
        switch (view.getId()) {
            case R.id.iv_add:
                mRlGone.setVisibility(View.VISIBLE);
                getView().findViewById(R.id.iv_add).setVisibility(View.GONE);
                break;
            case R.id.iv_close:
                mRlGone.setVisibility(View.GONE);
                break;
            case R.id.tv_fbgq:
                intent = new Intent(getActivity(), DemandIssueActivity.class);
                intent.putExtra(IntentKeyConstant.TYPE, 1);
                startActivityForResult(intent, 01);
                mRlGone.setVisibility(View.GONE);
                break;
            case R.id.tv_fbqg:
                intent = new Intent(getActivity(), DemandIssueActivity.class);
                intent.putExtra(IntentKeyConstant.TYPE, 2);
                startActivityForResult(intent, 01);
                mRlGone.setVisibility(View.GONE);
                break;
            case R.id.tv_zs:
                intent = new Intent(getActivity(), DemandIssueActivity.class);
                intent.putExtra(IntentKeyConstant.TYPE, 3);
                startActivityForResult(intent, 01);
                mRlGone.setVisibility(View.GONE);
                break;
            case R.id.tv_wdgq:
                intent = new Intent(getActivity(), SupplyDemandActivity.class);
                intent.putExtra(IntentKeyConstant.TYPE, mPosition);
//                intent.putExtra(IntentKeyConstant.ID, ((DemandSubTabFragment)mFragments.get(mPosition)).getcatId());
                startActivity(intent);
                mRlGone.setVisibility(View.GONE);
                break;
            case R.id.rl_gone:
                mRlGone.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK){
            if (requestCode == 01){
                EventBus.getDefault().post(new RefreshEven(1));

            }
        }
    }
}
